package df.servlet.process;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import df.bean.db.conn.DBConn;
import df.bean.db.conn.DBConnection;
import df.bean.db.table.PaymentMonthly;
import df.bean.obj.util.JDate;

public class ProcessCalculateSrvl extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private PaymentMonthly pm = null;
    private DBConnection connect = null;

	public void processRequest(HttpServletRequest request, HttpServletResponse response) {
		String mode = request.getParameter("mode");
		
		if(request.getParameter("mode") != null){
			if(mode.equals("paymentSummmary")){
				String date = request.getParameter("date");
				String hospitalCode = request.getParameter("hospitalCode");
				boolean check = true;
				DBConn dbConn = new DBConn();
				String bothJson= "";
				String sql = "";
				HashMap<String, String> hashData = new HashMap<String, String>();
		        sql="SELECT CODE FROM DOCTOR WHERE HOSPITAL_CODE = '"+hospitalCode+"'";
		        ArrayList<HashMap<String, String>> arrData = dbConn.listQueryData(dbConn.getConnection(), sql);
		        dbConn.closeDB("");
				this.connect = new DBConnection();
		        this.connect.connectToLocal();
		        if(arrData.size() > 0){
		        	for(int i=0;i<arrData.size();i++){
		            	//System.out.println("Test");
		        		hashData = arrData.get(i);
		                if (!Calculate(hospitalCode, hashData.get("CODE"), JDate.saveDate(date))) {
		                	check = false;
		                }else{
		                	check = true;
		                }
		        	}
					try {
				        //write Json	
				        bothJson += "{\"dataSize\" : \""+arrData.size()+"\",\"check\" : \""+check+"\"}";
						System.out.println("bothJson => " + bothJson);
				        response.setContentType("application/json"); 
						response.setCharacterEncoding("utf-8"); 
						response.getWriter().write(bothJson);
					} catch (IOException e) {
						System.out.println("Error Message => " + e.getMessage());
					}
		        }else{
		        	System.out.println("Data Not Found");
		        }
		        this.connect.Close();
			}
		}else{
			System.out.println("mode null don't work");
		}

	}

    public boolean Calculate(String hospitalCode, String doctorCode, String payDate) {
        boolean ret = false;
        pm = new PaymentMonthly(this.connect);
        if (pm.insertToPaymentSummmary(hospitalCode, payDate, doctorCode)>-1) {
            ret = true;    
        }else {  
            ret = false;   
        }
        return ret;
    } 
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

}
